import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class DisplayCustomers
{
	static final String DATABASE_URL = "jdbc:hsqldb:file:/home/andrew/dbTest/test1";
	
	public static void main(String args[])
	{
	// launch the application
	Connection connection = null; 
	Statement statement = null;
	ResultSet resultSet = null;
	
	//connect to database and query database
	try 
	{
		connection = DriverManager.getConnection(DATABASE_URL,"SA","");
		statement = connection.createStatement();
		resultSet=statement.executeQuery("SELECT FIRSTNAME,LASTNAME from CUSTOMER");
		
		ResultSetMetaData metaData = resultSet.getMetaData();
		int numberOfColumns = metaData.getColumnCount();
		System.out.println("Customer Names: \n");
		for (int i = 1; i <= numberOfColumns; i++)
			System.out.printf("%-8s\t", metaData.getColumnName(i));
			System.out.println();
			
			while (resultSet.next())
			{
				for (int i=1;i<= numberOfColumns; i++)
					System.out.printf("%-8s\t", resultSet.getObject(i));
					System.out.println();
				}
			}
			catch (SQLException sqlException)
			{
				sqlException.printStackTrace();
			}
			finally
			{
				try
				{
					resultSet.close();
					statement.close();
					connection.close();
				}
			catch (Exception exception)
			{
				exception.printStackTrace();
			}
		}
	}
}
